package com.study.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Linkman;
import com.study.model.pojo.User;
import com.study.services.CollectionService;
import com.study.services.CusmshfitServices;
import com.study.services.CustomerService;
import com.study.services.CustomerconsolidationServices;
import com.study.services.DictionaryService;
import com.study.services.LinkmanService;
import com.study.services.OrderfromServices;
import com.study.services.UserService;

@Controller
public class LinkmanController {
	@Autowired
	LinkmanService ls;
	@Autowired
	CustomerService cs;
	@Autowired
	UserService us ;

	OrderfromServices oredeservices;
	@Autowired
	CustomerconsolidationServices solid;
	@Autowired
	CusmshfitServices cusm;
	@Autowired
	DictionaryService ds;

	
	/**
	 * 采集页转化成客户
	 * @param dname
	 * @return 
	 */
/*	@GetMapping("/addlinkman")
	public ModelAndView addCustomerByColle(HttpServletRequest request) {
		String parameter = request.getParameter("id");
		JSONObject colle = service.selectBycid(parameter);
		ModelAndView mv = new ModelAndView("/customer/nwecumter");
		mv.addObject("colle", colle);
		return mv;
	}*/
	

	// 测试JPA方式的数据是否通畅
	/**Wcx
	 * 访问地址：http://localhost:8080/emp/add 点击提交之后，post提交，添加员工信息
	 * Wcx
	 * @param model
	 * @return 
	 */
	@GetMapping("linkman")
	public ModelAndView deptAdd() {
		// @GetMapping没有带斜杠/，默认的视图是jsp/add.jsp页面
		ModelAndView mv = new ModelAndView("customer/linkman");
		System.out.println("来了老弟linkman");
		return mv; 
	}
	
	/**
	 * 修改
	 * @param linid
	 * @return
	 */
	@GetMapping("updatelinkman")
	public ModelAndView updatelinkman(Integer id){
		int linid=id.intValue();
		ModelAndView mv = new ModelAndView("customer/linkmanadd");
		JSONObject js = ls.selectByID(linid);
		System.out.println(js);
		mv.addObject("linkmanaaa",js);

		return mv;
	}
	

	/**
	 * Wcx
	 * @param dname
	 */
	@GetMapping("addlin")
	public ModelAndView addDept(){	
		System.out.println("新建进来了");
		ModelAndView mv = new ModelAndView("customer/linkmanadd");
		JSONObject jsonObject = new JSONObject();
		mv.addObject("linkmanaaa",jsonObject);
		return mv;
	}
	
	@PostMapping("updatelin")
	@ResponseBody
	public ModelAndView lin(String linid,String phone,String name,String linkmantype,String cust,String business
			                ,String sex,String appellation,String type,String dept ,String duty,String email
			                , String workphone,String zip,String homephone,String wechat,String like,String idtype
			                ,String idnum,String remark,String photo,String habit,String interest,String trait
			                ,String social,String income ,String consumption,String custid,HttpServletRequest request) {				
		String useid= request.getSession().getAttribute("useid").toString();
		ModelAndView mv=new ModelAndView("/error");
		System.out.println("我来了linkmanPost");
		boolean updatelin = ls.updatelin(linid, phone,name,linkmantype,cust,business,sex,appellation,type,
				                        dept,duty,email,workphone,zip,homephone,wechat,like,idtype,idnum,
				                        remark,photo,habit,interest,trait,social,income,consumption, custid,useid);
		if (updatelin) {
			mv = new ModelAndView("/customer/linkman");
			System.out.println("哈哈");
		} else {
			 mv = new ModelAndView("/error");
		}

		System.err.println(useid);
		return mv;
	}
	/**
	 * Wcx
	 * 新建联系人
	 * @param name
	 * @param type
	 * @param phone
	 * @param consumption
	 * @return
	 */
	@PostMapping("addlinkman")
	@ResponseBody

	public String aa(String phone,String name,String linkmantype,String business
            ,String sex,String appellation,String type,String dept
            ,String duty,String email,String workphone,String zip
            ,String homephone,String wechat,String like,String idtype
            ,String idnum,String remark,String photo,String habit
            ,String interest,String trait,String social,String income
            ,String consumption,String custid) {
		System.out.println(duty+"sa-------------------------------------------------------------------");

		ModelAndView mv=new ModelAndView("/error");
		System.out.println("我来了linkmanPost");

		boolean result = ls.addlinkman(phone,name,linkmantype,business,sex,appellation,type,
                dept,duty,email,workphone,zip,homephone,wechat,like,idtype,idnum,
                remark,photo,habit,interest,trait,social,income,consumption, custid);

		if (result) {
			return "ok";
		} else {
			return "fail";
		}
		
	}
	
	/**
	 * Wcx
	 * 删除联系人
	 * @param cusid
	 * @return
	 */
	/*@PostMapping("customer/deletelinkman")
	@ResponseBody
	public ModelAndView delete(int[] cusids) {
		ModelAndView mv=new ModelAndView("/error");
		System.out.println("我来了deletelinkman");
		boolean result = cs.deleteCustomer(cusids);
		if (result) {
			mv = new ModelAndView("/customer/linkman");
		} else {
			 mv = new ModelAndView("/error");
		}
		return mv;
	}*/
     /**
      * Wcx
      * 带条件的分页查询
      * @param input
      * @param selects
      * @param currentPage
      * @param pagesize
      * @param session
      * @param request
      * @return
      */
	@PostMapping("customer/TJlinkman")
	@ResponseBody
	public String getForm3(String input,String selects,int currentPage,int pagesize,HttpSession session,HttpServletRequest request) {
		//获取当前登录员工id
		String useid= request.getSession().getAttribute("useid").toString();
		List<JSONObject> list = ls.selectToshow(currentPage,pagesize,input,selects,useid);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		return json;
	}	
	
	
	/**
	 * 联系人视图
	 * 
	 * @return
	 */
	@RequestMapping("/linview")
	public ModelAndView person(Model model, HttpServletRequest request, HttpSession session) {
		String id = request.getParameter("id");
		ModelAndView mv = new ModelAndView("customer/linView");
		List<JSONObject> list = ls.selectOne(id);
		System.out.println(list.get(0).getString("consumption"));
		mv.addObject("lookLinkman", list);
		System.out.println(list+"            hahahahahqha1");
		return mv;
	}
	
	
	
	
}
